package org.example.tool;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author sunshuai
 * @date 2025/9/19 15:42
 * @description:
 */
public class ImageUtils {

    public static boolean isSimpleColorImg(String imgPath, float percent) throws IOException {
        BufferedImage src = ImageIO.read(new File(imgPath));
        return ImageUtils.isSimpleColorImg(src,percent);
    }

    /**
     * 检测图片是否存在黑底
     * @param bufferedImage
     * @param percent 百分比
     * @return
     * @throws IOException
     */
    public static boolean isSimpleColorImg(BufferedImage bufferedImage, float percent) throws IOException {
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();
        int count = 0, pixel = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                pixel = bufferedImage.getRGB(i, j);
                Color color = new Color(pixel);
                int R = color.getRed();
                int G = color.getGreen();
                int B = color.getBlue();
                if(R < 30 && G < 30 && B < 30) {
                    count++;
                }
                if(count * 100 / (height * width) > percent) {
                    return true;
                }
            }
        }
        return false;
    }

}
